package com.link.base.base.sms.controller;

import com.link.base.sms.model.SendResult;
import com.link.base.base.sms.model.SmsBack;
import com.link.base.base.sms.model.SmsResultState;
import com.link.base.base.sms.service.SendResultService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短信发送记录
 *
 * @Author: liulu
 * @Date:15:37 2018/11/5
 */
@Controller
@RequestMapping("/link/sendResult")
public class SendResultController extends BasicController<SendResult> {

    @Resource
    private SendResultService sendResultService;

    @Override
    public BasicService<SendResult> getBasicService() throws Exception {
        return sendResultService;
    }

    /**
     * SMS回调接口
     *
     * @author 刘鲁
     */
    @RequestMapping(value = "/smsCallback")
    @ResponseBody
    public Map<String, Object> smsCallback(@RequestBody List<SmsResultState> entity, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(5);

        try {
            String id = request.getParameter("id");
            System.out.print(id);
            SendResult sendResult;
            for (SmsResultState state : entity) {
                sendResult = new SendResult(state.getSid().toString(), state.getUserReceiveTime(),
                        state.getReportStatus(), state.getErrmsg(), state.getDescription());
                sendResultService.updateSmsId(sendResult);
            }

            result.put("result", 0);
            result.put("errmsg", "OK");
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("result", 1);
            result.put("errmsg", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("result", 2);
            result.put("errmsg", e.getMessage());
        }
        return result;
    }

    /**
     * 短信回复回调接口
     *
     * @author lujiang
     * @date 2019-04-04
     * @param smsBack 短信回复json对象
     * @return
     */
    @RequestMapping(value = "/smsback")
    @ResponseBody
    public Map<String, Object> smsback(@RequestBody SmsBack smsBack) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            sendResultService.smsback(smsBack);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("errmsg", e.getMessage());
        }
        return result;
    }

    /**
     * 查询短信记录
     *
     * @author lujiang
     * @date 2019-04-04
     * @param sendResult 短信记录对象
     * @return
     */
    @RequestMapping(value = "/queryByMobileExamplePage")
    @ResponseBody
    public Map<String, Object> queryByMobileExamplePage(@JsonParam QueryParams qps,@JsonParam  SendResult sendResult,
                                                        HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.preDealSecurity(request);
            SendResult temp = BasicModel.transformClass(sendResult, qps);
            this.beforQueryAllOrExample(temp, request);
            this.beforQueryExample(temp, request);
            List<SendResult> list = sendResultService.queryByMobileExamplePage(temp);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount(temp, list.size()));
            result.put("success", true);
            result.put("rows", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("errmsg", e.getMessage());
        }
        return result;
    }
}
